using System.Collections.Generic;

namespace Terra.Core.Common.ErrorCodes.Models;

/// <summary>
/// 错误码信息
/// </summary>
public class ErrorCodeInfo
{
    /// <summary>
    /// 错误码
    /// </summary>
    public string Code { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 错误描述
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// 错误类型
    /// </summary>
    public string Type { get; set; }

    /// <summary>
    /// 错误级别
    /// </summary>
    public ErrorSeverity Severity { get; set; } = ErrorSeverity.Error;

    /// <summary>
    /// 解决方案
    /// </summary>
    public string Solution { get; set; }

    /// <summary>
    /// 错误参数
    /// </summary>
    public IDictionary<string, object> Parameters { get; set; }

    /// <summary>
    /// 格式化错误消息
    /// </summary>
    /// <returns>格式化后的错误消息</returns>
    public string FormatMessage()
    {
        if (Parameters == null || Parameters.Count == 0)
        {
            return Message;
        }

        try
        {
            return string.Format(Message, Parameters.Values.ToArray());
        }
        catch
        {
            return Message;
        }
    }
}